package com.bruce.spring.http.service.controller;

import com.bruce.spring.http.api.ProtocolApi;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.IOException;

/**
 * Created by bruce in 2025/6/26 01:43
 */
@Slf4j
@RestController
public class ProtocolController implements ProtocolApi {

    public String protocol() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse resp = requestAttributes.getResponse();

        String protocol = request.getProtocol();

        // log.info("protocol:{}", protocol);

        try {
            resp.flushBuffer();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return protocol;
    }

}
